#!F:/Python/python.exe
#coding=utf-8

import skyservice, skyServantLocator
from SimpleXMLRPCServer import SimpleXMLRPCServer, SimpleXMLRPCRequestHandler, CGIXMLRPCRequestHandler
from SocketServer import ThreadingMixIn
import types

#class SessionManager():
#    def __init__ (self):
#        self._sessPool = {}
#    def isSessActive(self, sessionID):
#        print self._sessPool
#        return sessionID in self._sessPool
#    def handleSession(self, id):
#        if self.isSessActive(id):
#            return False
#        else:
#            self._sessPool[id] = None
#            print self._sessPool
#            return True

#sessmgr = SessionManager()

locator = skyServantLocator.SkyEvictor()

class ISkySec(SimpleXMLRPCRequestHandler):  
#    def __init__(self, locator):
#        self.locator_ = locator 

    def logIn(self, id, pwd, deviceInfo):
#        print self.client_address

        secManager = skyservice.SkySecManager()
        if secManager.logIn(id, pwd):
            return locator.login(id)

    def signIn(self, info):  
        secManager = skyservice.SkySecManager()
        return secManager.signIn(info)
    
    def check(self, id):
        print locals()
        return True

    def getCode(self, id):
        print locals()
        return True    

    def exit(self, session):    
        print "exit:", locals()  
        return locator.finished(session)       

    def getNext(self, pageNum, pageSize, session):
        pimManager = locator.locate(session)
        pimManager.getNext(pageNum, pageSize)
       
    def find(self, session, context, pageNum, pageSize, type):       
        pimManager = locator.locate(session)
        if pimManager != None:
            totalsize, listAll = pimManager.find(context, pageNum, pageSize, type)
            return [totalsize, listAll]
        
#        if sessmgr.isSessActive(session) :
#            xxx = skyservice.SkyPimManager(session)
#            totalsize, listAll = xxx.find(context, pageNum, pageSize, type)
#            print totalsize
#            print listAll
#            return [totalsize, listAll]
#        else:
#            raise 'The User %s is InActive!\n' %(id,)


#===============================================================================
#        pimManager = locator.locate(session)
#        if pimManager != None:
#            totalsize, listAll = pimManager.find(context, pageNum, pageSize, type)
#            return [totalsize, listAll]
#===============================================================================
        
    def delete(self, session, idList):
        pimManager = locator.locate(session)
        if pimManager != None:
            return pimManager.delete(idList)

    def add(self, session, contactList):
        pimManager = locator.locate(session)
        if pimManager != None:
            return pimManager.add(contactList)

    def update(self, session, contactList):
        pimManager = locator.locate(session)
        if pimManager != None:
            return pimManager.update(contactList)

    def size(self, session):
        return 0

    def lastModified(self, session):
        pimManager = locator.locate(session)
        if pimManager != None:
            return pimManager.lastModified()

    def beginExport(self, accounts):
        pass
#        xxx = skyservice.SkyPimManager(accounts)
#        pimManager.beginExport()
    
    def endExport(self, accounts):
        pass
#        xxx = skyservice.SkyPimManager(accounts)
#        pimManager.endExport()
    
    def export(self, accounts, contacts):
        pass
#        xxx = skyservice.SkyPimManager(accounts)
#        pimManager.export(contacts)

    def getInfo(self, session):
        pimManager = locator.locate(session)
        if pimManager != None:
            isucceed,item = pimManager.getInfo()
            return item

    def setInfo(self, session, contact):
        pimManager = locator.locate(session)
        if pimManager != None:
            return pimManager.setInfo(contact)
    
    def incrementFrequency(self, session, id, sceneIdx):
        pimManager = locator.locate(session)
        if pimManager != None:
            return pimManager.incrementFrequency(id, sceneIdx)
    
    def toNoneString(self):
        return None
    
    def _dispatch(self, method, params):
        func = getattr(self, method, None)
        if func is not None:
            return func(*params)
        else:
            raise Exception('method "%s" is not supported' % method)
        

class ThreadingServer(ThreadingMixIn, SimpleXMLRPCServer):
    pass

#locator = skyServantLocator.SkyEvictor()
#pim = SkyPimI(locator)
#sec = ISkySec(locator)
# this model is for XML-RPC but not CGI begin. 
serveraddr = ('', 8080)
srvr = ThreadingServer(serveraddr, ISkySec)
#srvr.register_instance(ISkySec()
#srvr.register_instance(pim)
#srvr.regoster_function()
srvr.register_introspection_functions()
print "Server is in Service!"
srvr.serve_forever()
#end

